#!/bin/sh

## the default init script for commons distro

set -x -e -u

. /etc/os-release

MIRROR=repo.huaweicloud.com
MIRROR=mirrors.aliyun.com
MIRROR=mirrors.tuna.tsinghua.edu.cn

if [ "$ID" = "alpine" ]; then
  # Alpine tar.gz image has wrong permissions on the "/" aka. root folder, 
  # this prevents su from working, so only "install --root" is working.
  # /bin/chmod 755 /
  sed -i.bak -E "s#https?://dl-cdn\.alpinelinux\.org#http://$MIRROR#g" /etc/apk/repositories
elif [ "$ID" = "ubuntu" ]; then
  sed -i.bak -E "s#https?://(archive|security)\.ubuntu\.com#http://$MIRROR#g"  /etc/apt/sources.list
  apt-get update
elif [ "$ID" = "debian" ]; then
  sed -i.bak -E "s#https?://(deb|security|ftp)\.debian\.org#http://$MIRROR#g" /etc/apt/sources.list
  apt-get update
elif [ "$ID" = "fedora" ] || [ "$ID" = "fedoraremixforwsl" ]; then
  sed -e 's|^metalink=|#metalink=|g' \
    -e 's|^#baseurl=http://download.example/pub/fedora/linux|baseurl=http://mirrors.tuna.tsinghua.edu.cn/fedora|g' \
    -i.bak /etc/yum.repos.d/fedora*.repo
#    /etc/yum.repos.d/fedora.repo 
#    /etc/yum.repos.d/fedora-modular.repo 
#    /etc/yum.repos.d/fedora-updates.repo 
#    /etc/yum.repos.d/fedora-updates-modular.repo 
#    /etc/yum.repos.d/fedora-updates-testing.repo 
  dnf makecache
fi

touch ~/.profile
cat >> ~/.profile <<EOF
# wsl settins
PS1='\\[\\033[01;32m\\]\${WSL_DISTRO_NAME}\\[\\033[00m\\]:\\[\\033[01;34m\\]\\W\\[\\033[00m\\]\\$ '
alias ll='ls -la'
alias npp='start_npp() { /mnt/d/tools/notepadpp/notepad++.exe "\$(wslpath -w \$*)"; }; start_npp'
EOF

mkdir -p ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple/
#index-url = https://mirrors.cloud.tencent.com/pypi/simple/
#index-url = https://mirrors.tuna.tsinghua.edu.cn/pypi/simple/
#index-url = https://mirrors.aliyun.com/pypi/simple/
#index-url = https://mirrors.ustc.edu.cn/pypi/simple/

extra-index-urls = 
    https://repo.huaweicloud.com/repository/pypi/simple/
    https://mirrors.cloud.tencent.com/pypi/simple/
    https://mirrors.tuna.tsinghua.edu.cn/pypi/simple/
    https://mirrors.aliyun.com/pypi/simple/
    https://mirrors.ustc.edu.cn/pypi/simple/
EOF

mkdir -p /etc/containers
cat > /etc/containers/registries.conf <<EOF
unqualified-search-registries = ["docker.io"]


[[registry]]
prefix = "docker.io"
location = "docker.io"

[[registry.mirror]]
location = "docker.xuanyuan.me"

[[registry.mirror]]
location = "docker.1ms.run"

[[registry.mirror]]
location = "docker.m.daocloud.io"
EOF

cat > ~/.condarc <<EOF
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF

mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://docker.xuanyuan.me",
    "https://docker.1ms.run",
    "https://docker.m.daocloud.io"
    ]
}
EOF
